package export.strength
{
	import com.dusk.util.TextUtil;
	import com.game.data.vo.EquipmentVO;
	import com.game.data.vo.PlayerDataVO;
	import com.game.event.GameEvent;
	import com.game.manager.EventManager;
	import com.game.module.backpack.vo.BackPackEvent;
	import com.game.view.component.icon.RoleEquipIcon;
	
	import config.Config;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class Making extends Sprite
	{
		
		public function Making(param1:PlayerDataVO)
		{
			super();
			TextUtil.redrawTxtOld(this, this.needRedrawText);
			this.needAry = [];
			this.player = param1;
			this.gc = Config.getIns();
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
		}
		
		public var makingbook:Sprite;
		public var needmaterial1:Sprite;
		public var needmaterial2:Sprite;
		public var material1:Sprite;
		public var material2:Sprite;
		public var material3:Sprite;
		public var makeObj:Sprite;
		public var txthas1:TextField;
		public var txtneed1:TextField;
		public var txthas2:TextField;
		public var txtneed2:TextField;
		public var txt_needlh:TextField;
		public var txt_name:TextField;
		public var btn_help:SimpleButton;
		public var dzbtn:SimpleButton;
		private var gc:Config;
		private var player:PlayerDataVO;
		private var curmbook:EquipmentVO;
		private var needAry:Array;
		private var tag:Boolean = true;
		private var outList:Array = [];
		private var needRedrawText:Array = ["txthas1", "txtneed1", "txthas2", "txtneed2", "txt_needlh", "txt_name"];
		
		private function addNeedMaterial(param1:Array):void
		{
			var _loc2_:* = null;
			this.cleanNeedMaterial();
			var _loc3_:uint = param1.length;
			var _loc4_:int = 0;
			while (_loc4_ < _loc3_)
			{
				if (param1[_loc4_] != null)
				{
					_loc2_ = new RoleEquipIcon(param1[_loc4_].equip);
					this["needmaterial" + (_loc4_ + 1)].addChild(_loc2_);
					_loc2_ = null;
				}
				_loc4_++;
			}
		}
		
		private function cleanNeedMaterial():void
		{
			var _loc1_:* = null;
			var _loc2_:int = 0;
			while (_loc2_ < 2)
			{
				if (this["needmaterial" + (_loc2_ + 1)].getChildByName("curzb"))
				{
					_loc1_ = this["needmaterial" + (_loc2_ + 1)].getChildByName("curzb") as RoleEquipIcon;
					this["needmaterial" + (_loc2_ + 1)].removeChild(_loc1_);
					_loc1_ = null;
				}
				_loc2_++;
			}
		}
		
		private function removeEquipFormBack(param1:EquipmentVO, param2:int, param3:uint = 1):void
		{
			var _loc4_:int = 0;
			trace("equip.getENum()= " + param1.num);
			trace("num:" + param3);
			var _loc5_:EquipmentVO = this.player.getEquipInBagByName(param1.equipId);
			if (param2 == 1)
			{
				if (param1.num > 1)
				{
					param1.modifyNum(-1);
				}
				else if ((_loc4_ = int(this.player.bagEquipList.indexOf(param1))) != -1)
				{
					this.player.bagEquipList.splice(_loc4_, 1);
				}
			}
			else if (param2 == 2)
			{
				if (param1.num > param3)
				{
					param1.modifyNum(-param3);
				}
				else if ((_loc4_ = int(this.player.bagStuffList.indexOf(param1))) != -1)
				{
					this.player.bagStuffList.splice(_loc4_, 1);
				}
			}
		}
		
		private function setTxt():void
		{
			var _loc1_:* = 0;
			var _loc2_:int = 0;
			this.txthas1.text = " ";
			this.txtneed1.text = " ";
			this.txthas2.text = " ";
			this.txtneed2.text = " ";
			this.txt_needlh.text = " ";
			this.txt_name.text = " ";
			if (this.needAry == null)
			{
				return;
			}
			if (this.curmbook)
			{
				_loc1_ = uint(this.needAry.length);
				_loc2_ = 0;
				while (_loc2_ < _loc1_)
				{
					this["txthas" + (_loc2_ + 1)].text = this.player.getStuffNumByName(this.needAry[_loc2_].equip.equipId) + " ";
					this["txtneed" + (_loc2_ + 1)].text = this.needAry[_loc2_].count + " ";
					_loc2_++;
				}
				this.txt_needlh.text = this.needLHValueByQuality(this.curmbook.quality) + " ";
			}
			trace("this.makeObj:" + this.makeObj);
			var _loc3_:RoleEquipIcon = this.makingbook.getChildByName("curzb") as RoleEquipIcon;
			if (_loc3_)
			{
				var _loc4_:String = _loc3_.getEquipVO().equipId.slice(0, _loc3_.getEquipVO().equipId.indexOf("zzs"));
				if (gc.allEquip.findByName(_loc4_))
				{
					this.txt_name.text = gc.allEquip.findByName(_loc4_).name;
				}
			}
		}
		
		private function needLHValueByQuality(param1:String):uint
		{
			switch (param1)
			{
				case "粗 糙":
					return 50 + Math.random();
				case "普 通":
					return 100 + Math.random();
				case "优 秀":
					return 200 + Math.random();
				case "精 良":
					return 400 + Math.random();
				case "史 诗":
					return 800 + Math.random();
				case "邪 灵":
					return 1600 + Math.random();
				case "魂 器":
					return 3200 + Math.random();
				case "传 说":
					return 6400 + Math.random();
				case "神 器":
					return 12800 + Math.random();
				default:
					return 200000;
			}
		}
		
		private function getOutList():void
		{
			var _loc1_:* = null;
			this.outList = [];
			if (this.curmbook)
			{
				this.outList.push({
					"dequip": this.curmbook,
					"count" : 1
				});
			}
			var _loc2_:int = 0;
			while (_loc2_ < 3)
			{
				if (this["material" + (_loc2_ + 1)].getChildByName("curzb"))
				{
					_loc1_ = this["material" + (_loc2_ + 1)].getChildByName("curzb") as RoleEquipIcon;
					this.outList.push({
						"dequip": _loc1_.getMyEquipObj(),
						"count" : 1
					});
					_loc1_ = null;
				}
				_loc2_++;
			}
		}
		
		private function achieveWhichProduce(param1:String):EquipmentVO
		{
			var _loc2_:*;//生成的目标装备
			var _loc3_:* = null;//放入的宝石
			var _loc4_:* = undefined;//加成属性存储
			var _loc5_:String = param1.substr(0, param1.length - 3);
			_loc2_ = this.gc.allEquip.findByName(_loc5_);
			var _loc6_:int = 0;
			while (_loc6_ < 3)
			{
				_loc3_ = this["material" + (_loc6_ + 1)].getChildByName("curzb") as RoleEquipIcon;
				if (_loc3_)
				{
					var _loc1_:String = _loc3_.getMyEquipObj().equipId;//宝石名字存储
					_loc4_ = this.randomGemAttributeByGemName(_loc1_);
					trace("a------>>" + _loc4_);
					if (_loc1_.indexOf("sms") >= 0)
					{
						_loc2_.setehp(_loc2_.getehp() + _loc4_);
					}
					if (_loc1_.indexOf("mfs") >= 0)
					{
						_loc2_.setemp(_loc2_.getemp() + _loc4_);
					}
					if (_loc1_.indexOf("gjs") >= 0)
					{
						_loc2_.seteatt(_loc2_.geteatt() + _loc4_);
					}
					if (_loc1_.indexOf("fys") >= 0)
					{
						_loc2_.setedef(_loc2_.getedef() + _loc4_);
					}
					if (_loc1_.indexOf("tlz") >= 0)
					{
						_loc2_.setmagicdef(_loc2_.getmagicdef() + _loc4_);
					}
					if (_loc1_.indexOf("llz") >= 0)
					{
						_loc2_.setecrit(_loc2_.getecrit() + _loc4_);
					}
					if (_loc1_.indexOf("hlz") >= 0)
					{
						_loc2_.seteahp(_loc2_.geteahp() + _loc4_);
					}
					if (_loc1_.indexOf("flz") >= 0)
					{
						_loc2_.setemiss(_loc2_.getemiss() + _loc4_);
					}
					if (_loc1_.indexOf("slz") >= 0)
					{
						_loc2_.seteamp(_loc2_.geteamp() + _loc4_);
					}
					if (_loc1_.indexOf("clz") >= 0)
					{
						_loc2_.setdeephit(_loc2_.getdeephit() + _loc4_);
					}
				}
				_loc6_++;
			}
			return _loc2_;
		}
		
		private function randomGemAttributeByGemName(param1:String):*
		{
			switch (param1)
			{
				case "sms1":
					return Math.round(1 + Math.random() * 20);
				case "sms2":
					return Math.round(40 + Math.random() * 70);
				case "sms3":
					return Math.round(150 + Math.random() * 50);
				case "mfs1":
					return Math.round(1 + Math.random() * 20);
				case "mfs2":
					return Math.round(40 + Math.random() * 70);
				case "mfs3":
					return Math.round(150 + Math.random() * 50);
				case "gjs1":
					return Math.round(1 + Math.random() * 10);
				case "gjs2":
					return Math.round(15 + Math.random() * 5);
				case "gjs3":
					return Math.round(25 + Math.random() * 15);
				case "fys1":
					return Math.round(1 + Math.random() * 2);
				case "fys2":
					return Math.round(2 + Math.random() * 4);
				case "fys3":
					return Math.round(5 + Math.random() * 7);
				case "wptlz":
					return 0.01 + Math.random() * 0.01;
				case "wpllz":
					return 0.01 + Math.random() * 0.02;
				case "wpflz":
					return 0.01 + Math.random() * 0.01;
				case "wpclz":
					return 0.01 + Math.random() * 0.02;
				case "wpslz":
					return 1 + Math.round(Math.random());
				case "wphlz":
					return 1 + Math.round(Math.random() * 2);
				default:
					return 0;
			}
		}
		
		private function clearAllThingExceptMakeObject(param1:Boolean):void
		{
			var _loc2_:* = null;
			this.curmbook = null;
			if (this.makingbook.getChildByName("curzb"))
			{
				_loc2_ = this.makingbook.getChildByName("curzb") as RoleEquipIcon;
				if (!param1)
				{
					this.gc.putQhsInBackPack(this.player, _loc2_.getMyEquipObj().equipId);
				}
				this.makingbook.removeChild(_loc2_);
				_loc2_ = null;
			}
			this.cleanNeedMaterial();
			var _loc3_:int = 0;
			while (_loc3_ < 3)
			{
				if (this["material" + (_loc3_ + 1)].getChildByName("curzb"))
				{
					_loc2_ = this["material" + (_loc3_ + 1)].getChildByName("curzb") as RoleEquipIcon;
					this["material" + (_loc3_ + 1)].removeChild(_loc2_);
					if (!param1)
					{
						this.gc.putQhsInBackPack(this.player, _loc2_.getMyEquipObj().equipId);
					}
					_loc2_ = null;
				}
				_loc3_++;
			}
			this.setTxt();
		}
		
		private function added(param1:Event):void
		{
			if (this.tag)
			{
				this.tag = false;
				this.outList = [];
				this.gc.eventManager.addEventListener("SimpleClick", this.receiveObj);
				this.dzbtn.addEventListener(MouseEvent.CLICK, this.dzClick);
				this.makeObj.addEventListener(MouseEvent.CLICK, this.removeMakeObj);
				this.material1.addEventListener(MouseEvent.CLICK, this.onClick);
				this.material2.addEventListener(MouseEvent.CLICK, this.onClick);
				this.material3.addEventListener(MouseEvent.CLICK, this.onClick);
			}
		}
		
		private function removed(param1:Event):void
		{
			this.gc.eventManager.removeEventListener("SimpleClick", this.receiveObj);
			this.dzbtn.removeEventListener(MouseEvent.CLICK, this.dzClick);
			this.makeObj.removeEventListener(MouseEvent.CLICK, this.removeMakeObj);
			this.material1.removeEventListener(MouseEvent.CLICK, this.onClick);
			this.material2.removeEventListener(MouseEvent.CLICK, this.onClick);
			this.material3.removeEventListener(MouseEvent.CLICK, this.onClick);
			this.clearAllThingExceptMakeObject(false);
		}
		
		private function receiveObj(param1:GameEvent):void
		{
			var _loc2_:* = null;
			var _loc3_:int = 0;
			var _loc4_:RoleEquipIcon;
			if ((_loc4_ = param1.data[0] as RoleEquipIcon).getEquipVO().findUsedByShowId() == "zzs")
			{
				this.curmbook = null;
				this.curmbook = _loc4_.getEquipVO();
				_loc2_ = this.makingbook.getChildByName("curzb") as RoleEquipIcon;
				if (_loc2_ == null)
				{
					this.makingbook.addChild(_loc4_);
				}
				else
				{
					this.makingbook.removeChild(_loc2_);
					this.gc.putQhsInBackPack(this.player, _loc2_.getMyEquipObj().equipId);
					this.makingbook.addChild(_loc4_);
					_loc2_ = null;
				}
				this.removeEquipFormBack(_loc4_.getEquipVO(), 2);
				this.needAry = this.gc.allEquip.findNeedMaterialByName(this.curmbook.equipId);
				this.addNeedMaterial(this.needAry);
			}
			else if (_loc4_.getEquipVO().findUsedByShowId() == "bs")
			{
				if (this.curmbook)
				{
					_loc3_ = 1;
					while (_loc3_ <= 3)
					{
						if (this["material" + _loc3_].getChildByName("curzb") == null)
						{
							this["material" + _loc3_].addChild(_loc4_);
							this.removeEquipFormBack(_loc4_.getEquipVO(), 2);
							break;
						}
						_loc3_++;
					}
				}
				else
				{
					trace("current making book is null");
				}
			}
			this.removeMakeObj(null);
			this.getOutList();
			this.setTxt();
			EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
		}
		
		private function onClick(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			if (param1.currentTarget.getChildByName("curzb") != null)
			{
				_loc2_ = param1.currentTarget.getChildByName("curzb") as RoleEquipIcon;
				param1.currentTarget.removeChild(_loc2_);
				switch (_loc2_.getMyEquipObj().type)
				{
					case "zbwq":
					case "zbfj":
					case "zbsp":
					case "zbfb":
						this.player.bagEquipList.push(_loc2_.getMyEquipObj());
						break;
					case "wpqhs":
						if (_loc2_.getMyEquipObj().ename.indexOf("强化石") != -1)
						{
							this.gc.putQhsInBackPack(this.player, _loc2_.getMyEquipObj().equipId);
							break;
						}
						break;
					case "zbwp":
						if (_loc2_.getMyEquipObj().findUsedByShowId() == "bs")
						{
							this.gc.putQhsInBackPack(this.player, _loc2_.getMyEquipObj().equipId);
							break;
						}
				}
				EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
				_loc2_ = null;
			}
		}
		
		private function dzClick(param1:MouseEvent):void
		{
			var _loc2_:* = 0;
			var _loc3_:int = 0;
			var _loc4_:* = null;
			var _loc5_:* = null;
			if (this.curmbook == null)
			{
				return;
			}
			if (this.player.lhValue >= int(this.txt_needlh.text) && int(this.txthas1.text) >= int(this.txtneed1.text) && int(this.txthas2.text) >= int(this.txtneed2.text))
			{
				player.reduceLhValue(int(this.txt_needlh.text));
				StrengthEquipment(this.parent).setTxtlh();
				trace("needAry:" + this.needAry);
				_loc2_ = uint(this.needAry.length);
				_loc3_ = 0;
				while (_loc3_ < _loc2_)
				{
					this.outList.push({
						"dequip": this.player.getEquipInBagByName(this.needAry[_loc3_].equip.equipId),
						"count" : this.needAry[_loc3_].count
					});
					_loc5_ = this.player.getEquipInBagByName(this.needAry[_loc3_].equip.equipId);
					this.removeEquipFormBack(_loc5_, 2, this.needAry[_loc3_].count);
					_loc3_++;
				}
				this.needAry = null;
				_loc4_ = new RoleEquipIcon(this.achieveWhichProduce(this.curmbook.equipId));
				this.player.bagEquipList.push(_loc4_.getMyEquipObj());
				this.makeObj.addChild(_loc4_);
				this.clearAllThingExceptMakeObject(true);
				EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_NUMBER);
				this.gc.eventManager.dispatchEvent(new GameEvent("CHANGETXT"));
				this.gc.memory.setStorage();
			}
		}
		
		private function removeMakeObj(param1:MouseEvent):void
		{
			var _loc2_:* = this.makeObj.getChildByName("curzb") as RoleEquipIcon;
			if (_loc2_)
			{
				this.makeObj.removeChild(_loc2_);
				_loc2_ = null;
			}
		}
	}
}
